﻿@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Index.cshtml";
}



@*<div class="topPanel">
        <div class="toolbar">
            <div class="btn-group">
                <a class="btn btn-primary" onclick="$.reload()"><span class="glyphicon glyphicon-refresh"></span></a>
            </div>
            <div class="btn-group">
                <a id="NF-add" class="btn btn-primary dropdown-text" onclick="btn_add()"><i class="fa fa-plus"></i>新增股道</a>
            </div>
            <div class="operate">

            </div>
            <script>$('.toolbar').authorizeButton()</script>
        </div>
        <div class="search">
            <table>
                <tr>
                    <td>
                        <div class="input-group" style="padding-left:11px;padding-right:10px;">

                        </div>
                    </td>
                </tr>
            </table>
        </div>
    </div>*@
<style>
    table tr td {
        padding-left: 15px;
    }

    table tbody tr td {
        height: 20px;
    }

    .form-control {
        display: block;
        /*width: 100%;*/
        /*height: 34px;*/
        width: 53px;
        height: 20px;
        font-size: 10px;
        text-align: center;
        line-height: 1.42857;
        color: rgb(85, 85, 85);
        background-color: rgb(255, 255, 255);
        background-image: none;
        box-shadow: rgba(0, 0, 0, 0.075) 0px 1px 1px inset;
        padding: 8px 19.5px;
        border-width: 1px;
        border-style: solid;
        border-color: rgb(221, 221, 221);
        border-image: initial;
        border-radius: 4px;
        transition: border-color 0.15s ease-in-out 0s, box-shadow 0.15s ease-in-out 0s;
    }
</style>
<div class="gridPanel">

    <table id="gridList" class="table table-bordered table-hover" style="table-layout:fa">
        <thead>
            <tr>
                <th style="text-align:center">序号</th>
                <th style="text-align:center">股道</th>
                <th style="text-align:center">车号</th>
                <th style="text-align:center">车型</th>
                <th style="text-align:center">载重</th>
                <th style="text-align:center">品名</th>
                <th style="text-align:center">封号</th>
                <th style="text-align:center">到/发站</th>
                <th style="text-align:center">件数</th>
                <th style="text-align:center">重量</th>
                <th style="text-align:center">客户</th>
                <th style="text-align:center">备注</th>
                <th style="text-align:center">操作</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>
                    <input type="text" class="form-control xuhao" value="1">
                </td>
                <td>
                    <select class="form-control gudao"></select>
                </td>
                <td>
                    <input type="text" class="form-control chehao" value="">
                </td>
                <td>
                    @*<input type="text" class="form-control chexing" value="">*@
                    <select class="form-control chexing"></select>
                </td>
                <td>
                    <input type="text" class="form-control zaizhong" value="">
                </td>
                <td>
                    <select class="form-control pingming"></select>
                    @*<input type="text" class="form-control pingming" value="1">*@
                </td>
                <td>
                    <input type="text" class="form-control fenghao" value="">
                </td>
                <td>
                    <input type="text" class="form-control daofazhan" value="">
                </td>
                <td>
                    <input type="text" class="form-control jianshu" value="">
                </td>
                <td>
                    <input type="text" class="form-control zhongliang" value="">
                </td>
                <td>
                    <select class="form-control kehu"></select>
                    @*<input type="text" class="form-control kehu" value="1">*@
                </td>
                <td>
                    <input type="text" class="form-control beizhu" value="">
                </td>

                <td>
                    <input type="button" class="form-control addnewlen" value="添加一行" />
                    <input type="button" class="form-control" value="添加箱号" onclick="AddCarBox(this);" />
                </td>
            </tr>
        </tbody>
    </table>
    <div id="gridPager" style="text-align: center; display: block;">
        <input type="button" class="form-control addnewlen" value="添加一行" style="float: left;margin-left:5px" />
        <input type="button" class="form-control" id="submitId" value="确认添加" onclick="SubmitThisInfo();" style="float: left;margin-left:20px" />
    </div>
</div>
<script src="~/Content/js/guangbiao/GuangBiaoMove.js"></script>
<script>
    //鼠标移动上下左右换行
    tabTableInput("gridList", "text");

    var keyValue = decodeURI($.request("keyValue"));
    //alert(keyValue);
    var newJYDDIndex = 1;
    var gudaolist = '@ViewBag.gudaolist';

    var listobj;
    var optionstr = '';
    var pingming = '@ViewBag.pingminglist';

    var pinminglist;
    var pinmingliststr='';
    var kehu = '@ViewBag.thingsName';
    var kehulist;
    var kehuliststr = '';

    var cartypelist;
    var cartypeliststr = ''
    var cartype = '@ViewBag.carType'
    
    $(function() {
        var reg = new RegExp('&quot;', "g")
        gudaolist = gudaolist.replace(reg, "\"");
        pingming = pingming.replace(reg, "\"");
        kehu = kehu.replace(reg, "\"");
        cartype = cartype.replace(reg, "\"")

        listobj = JSON.parse(gudaolist);
        $.each(listobj, function (i) {
            if (listobj[i].Name == keyValue) {
                optionstr += "<option value='" + listobj[i].F_Id + "'>" + listobj[i].Name + "</option>";
            }
        });
        pinminglist = JSON.parse(pingming);
        $.each(pinminglist, function (i) {
            pinmingliststr += "<option value='" + pinminglist[i].Name + "'>" + pinminglist[i].Name + "</option>";
        });
        kehulist = JSON.parse(kehu);
        $.each(kehulist, function (i) {
            kehuliststr += "<option value='" + kehulist[i].Name + "'>" + kehulist[i].Name + "</option>";
        });
        cartypelist = JSON.parse(cartype);
        $.each(cartypelist, function (i) {
            cartypeliststr += "<option value='" + cartypelist[i].Name + "'>" + cartypelist[i].Name + "</option>";
        });
        $(".gudao").append(optionstr);
        $(".pingming").append(pinmingliststr);
        $(".kehu").append(kehuliststr);
        $(".chexing").append(cartypeliststr);

        $(".addnewlen").click(function () {
            var pingmingselect = $("#gridList tbody").children().last().find(".pingming").val();
            var daofazhan = $("#gridList tbody").children().last().find(".daofazhan").val();
            var kehuselect = $("#gridList tbody").children().last().find(".kehu").val();
            //var pingminglist = pinmingliststr.split("</option>");
            //console.log(pingminglist);
            pinmingliststr = '';
            pinminglist = JSON.parse(pingming);
            $.each(pinminglist, function (i) {
                if (pingmingselect == pinminglist[i].Name) {
                    pinmingliststr += "<option value='" + pinminglist[i].Name + "' selected = \"selected\">" + pinminglist[i].Name + "</option>";
                } else {
                    pinmingliststr += "<option value='" + pinminglist[i].Name + "'>" + pinminglist[i].Name + "</option>";
                }
            });
            kehuliststr = '';
            kehulist = JSON.parse(kehu);
            $.each(kehulist, function (i) {
                if (kehuselect == kehulist[i].Name) {
                    kehuliststr += "<option value='" + kehulist[i].Name + "' selected = \"selected\" >" + kehulist[i].Name + "</option>";
                } else {
                    kehuliststr += "<option value='" + kehulist[i].Name + "'>" + kehulist[i].Name + "</option>";
                }
            });

            cartypeliststr = '';
            cartype = JSON.parse(cartype);
            $.each(cartype, function (i) {
                if (kehuselect == cartype[i].Name) {
                    cartypeliststr += "<option value='" + cartype[i].Name + "' selected = \"selected\" >" + cartype[i].Name + "</option>";
                } else {
                    cartypeliststr += "<option value='" + cartype[i].Name + "'>" + cartype[i].Name + "</option>";
                }
            });
            newJYDDIndex += 1;
            $("#gridList tbody").append(
                '<tr>  <td><input type="text" class="form-control xuhao"value="' +
                newJYDDIndex +
                '" > </td>  <td> <select  class="form-control gudao" >' + optionstr + '</select></td> ' +
                '<td> <input type="text" class="form-control chehao"value="" ></td>' +
                '<td><select class="form-control chexing" >' + cartypeliststr + '</select> ' +
                '</td> <td> <input type="text" class="form-control zaizhong"value="" ></td>' +
                ' <td>   <select class="form-control pingming" >' + pinmingliststr + '</select> </td> ' +
                '<td> <input type="text" class="form-control fenghao"value="" > </td>' +
                '<td> <input type="text" class="form-control daofazhan"value="' + daofazhan+'">  </td>' +
                ' <td>  <input type="text" class="form-control jianshu" value=""> </td> ' +
                ' <td>  <input type="text" class="form-control zhongliang" value=""> </td> ' +
                '<td> <select class="form-control kehu" >' + kehuliststr+'</select>  </td>' +
                '<td><input type="text" class="form-control beizhu"value="" > </td> ' +
                '<td> <input type="button" class="form-control" value="删除当前行"  onclick=\'deleteTr(this);\'/><input type="button" class="form-control addnewlen" value="添加箱号"/> </td></tr>');
            //每次添加一行数据调用这个方法
            tabTableInput("gridList", "text");
        });

    });

    function deleteTr(nowTr) {
        $(nowTr).parent().parent().remove();
        newJYDDIndex = newJYDDIndex - 1;
    }
    function AddCarBox(nowTr) {
        debugger;
        var thisTrChehaoDate = $(nowTr).parent().parent().find(".chehao").val();
        if (thisTrChehaoDate == undefined || thisTrChehaoDate == null || thisTrChehaoDate == "") {
            top.alert("请在此列先填写车号", function (i) {
                top.close(i);
            });
            return;
        } else {
            //// 存储值：将对象转换为Json字符串
            ////sessionStorage.setItem('user', JSON.stringify(userEntity));
            //// 取值时：把获取到的Json字符串转换回对象
            ////var userJsonStr = sessionStorage.getItem('user');
            ////userEntity = JSON.parse(userJsonStr);
            ////console.log(userEntity.name); // => tom
            ////alert(thisTrChehaoDate);
               $.modalOpen({
                    id: "Form",
                    title: "箱号操作",
                   url: "/Project/PickCar/AddCarBox?keyValue=" + thisTrChehaoDate,
                    width: "650px",
                    height: "650px",
                    callBack: function (iframeId) {
                        top.frames[iframeId].SubmitThisInfo();
                    }
                });
        }
    }
    function SubmitThisInfo() {
        debugger;
        $("#submitId").attr("disabled", "disabled");
        var arr = [];
        var trs = $("#gridList tbody tr");
        if (trs.length <= 0) {
            top.alert("请添加数据", function (i) {
                top.close(i);
            });
            return false;
        }
        var flag = 0;
        trs.each(function () {
            debugger;
            var obj = {};
            obj.xuhao = $.trim($(this).find(".xuhao").val());
            obj.gudao = $.trim($(this).find(".gudao").val());
            obj.chehao = $.trim($(this).find(".chehao").val());
            obj.chexing = $.trim($(this).find(".chexing").val());
            obj.zaizhong = $.trim($(this).find(".zaizhong").val());
            obj.pingming = $.trim($(this).find(".pingming").val());
            obj.fenghao = $.trim($(this).find(".fenghao").val());
            obj.daofazhan = $.trim($(this).find(".daofazhan").val());
            obj.jianshu = $.trim($(this).find(".jianshu").val());
            obj.zhongliang = $.trim($(this).find(".zhongliang").val());
            obj.kehu = $.trim($(this).find(".kehu").val());
            obj.beizhu = $.trim($(this).find(".beizhu").val());
            if (!$.trim(obj.xuhao) || !$.trim(obj.gudao) || !$.trim(obj.chehao) || !$.trim(obj.pingming) || !$.trim(obj.zhongliang) || !$.trim(obj.kehu)) {
                flag = 2;
                return false;
            }
            arr.push(obj);
        });
        debugger;
        switch (flag) {
        case 2:
            top.alert("传入数据为空", function (i) {
                top.close(i);
            });
                $("#submitId").attr("disabled", false);
            return false;
            break;
        }
        $.ajax({
            url: '/Project/PickCar/AddGudaoCar',
            data: { jsonData: JSON.stringify(arr) },
            dataType: "text",
            type: "POST",
            success: function (data) {
                var dataobj = JSON.parse(data);
                if (dataobj.code == 0) {
                    top.alert("添加成功", function (i) {
                    });
                    $("#submitId").attr("disabled", false);
                    $.modalClose();
                }
                else {
                    top.alert(dataobj.msg, function (i) {
                        $("#submitId").attr("disabled", false);
                        layer.close(i);
                    });
                }
            }
        });
    }
</script>